# # 问题案例
# print(stu)
# print('hello')

# 解决办法：捕获异常
# try:
#     ...
# except BaseException as e:
#     ...
# else:
#     ...
# finally:
#     ...

# 案例1：重点掌握
try:
    print(stu)
except BaseException as e:  # BaseException 是python中所有异常的基类
    print(e)
print('hello')

# 案例2：
try:
    print(stu)
except Exception as e:  # Exception 是 BaseException 子类
    print(e)
print('hello')

# 案例3：了解一下
try:
    print(stu)
except NameError as e:  # Exception 是 BaseException 子类
    print(e)
print('hello')

# 案例4：else（了解一下
try:
    print(stu)
except Exception as e:
    print(e)
else:
    print('正常执行')
print('hello')

# 案例5：finally（了解一下
try:
    stu = 'ammy'
    print(stu)
except Exception as e:
    print(e)
else:
    print('正常执行')
finally:
    print('执行完毕')

# 扩展一下：了解一下
# raise 抛出异常
exp = Exception('0不能为除数')


def division(a, b):
    if b == 0:
        raise exp


try:
    division(10, 0)
except Exception as e:
    print(e)

# 综合案例：
print('---------华丽丽----------')
list_a = [123, 34, 11, 0, 10]
# 遍历列表，打印10除以每个值的结果，如果有异常，请捕获
i = 1
for num in list_a:
    try:
        print(f'第{i}次执行，结果是:', end='')
        print(10 / num)
    except Exception as e:
        print(f'出现异常：{e}')
    else:
        print('正常执行')
    finally:
        print('执行完成')
        i += 1
